// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.iso;

import java.util.ArrayList;
import zombie.characters.IsoPlayer;
import zombie.core.opengl.RenderSettings;
import zombie.iso.areas.IsoBuilding;
import zombie.iso.objects.IsoLightSwitch;

public class IsoLightSource {
    public static int NextID = 1;
    public int ID;
    public int x;
    public int y;
    public int z;
    public float r;
    public float g;
    public float b;
    public float rJNI;
    public float gJNI;
    public float bJNI;
    public int radius;
    public boolean bActive;
    public boolean bWasActive;
    public boolean bActiveJNI;
    public int life = -1;
    public int startlife = -1;
    public IsoBuilding localToBuilding;
    public boolean bHydroPowered = false;
    public ArrayList<IsoLightSwitch> switches = new ArrayList(0);
    public IsoChunk chunk;
    public Object lightMap;

    public IsoLightSource(int _x, int _y, int _z, float _r, float _g, float _b, int _radius) {
        this.x = _x;
        this.y = _y;
        this.z = _z;
        this.r = _r;
        this.g = _g;
        this.b = _b;
        this.radius = _radius;
        this.bActive = true;
    }

    public IsoLightSource(int _x, int _y, int _z, float _r, float _g, float _b, int _radius, IsoBuilding building) {
        this.x = _x;
        this.y = _y;
        this.z = _z;
        this.r = _r;
        this.g = _g;
        this.b = _b;
        this.radius = _radius;
        this.bActive = true;
        this.localToBuilding = building;
    }

    public IsoLightSource(int _x, int _y, int _z, float _r, float _g, float _b, int _radius, int _life) {
        this.x = _x;
        this.y = _y;
        this.z = _z;
        this.r = _r;
        this.g = _g;
        this.b = _b;
        this.radius = _radius;
        this.bActive = true;
        this.startlife = this.life = _life;
    }

    public void update() {
        IsoGridSquare isoGridSquare = IsoWorld.instance.CurrentCell.getGridSquare(this.x, this.y, this.z);
        if (!this.bHydroPowered || IsoWorld.instance.isHydroPowerOn() || isoGridSquare != null && isoGridSquare.haveElectricity()) {
            if (this.bActive) {
                if (this.localToBuilding != null) {
                    this.r = RenderSettings.getInstance().getAmbientForPlayer(IsoPlayer.getPlayerIndex()) * 0.7F;
                    this.g = RenderSettings.getInstance().getAmbientForPlayer(IsoPlayer.getPlayerIndex()) * 0.7F;
                    this.b = RenderSettings.getInstance().getAmbientForPlayer(IsoPlayer.getPlayerIndex()) * 0.7F;
                }

                if (this.life > 0) {
                    this.life--;
                }

                if (this.localToBuilding != null && isoGridSquare != null) {
                    this.r = RenderSettings.getInstance().getAmbientForPlayer(IsoPlayer.getPlayerIndex()) * 0.8F * IsoGridSquare.rmod * 0.7F;
                    this.g = RenderSettings.getInstance().getAmbientForPlayer(IsoPlayer.getPlayerIndex()) * 0.8F * IsoGridSquare.gmod * 0.7F;
                    this.b = RenderSettings.getInstance().getAmbientForPlayer(IsoPlayer.getPlayerIndex()) * 0.8F * IsoGridSquare.bmod * 0.7F;
                }

                for (int int0 = this.x - this.radius; int0 < this.x + this.radius; int0++) {
                    for (int int1 = this.y - this.radius; int1 < this.y + this.radius; int1++) {
                        for (int int2 = 0; int2 < 8; int2++) {
                            isoGridSquare = IsoWorld.instance.CurrentCell.getGridSquare(int0, int1, int2);
                            if (isoGridSquare != null && (this.localToBuilding == null || this.localToBuilding == isoGridSquare.getBuilding())) {
                                LosUtil.TestResults testResults = LosUtil.lineClear(
                                    isoGridSquare.getCell(), this.x, this.y, this.z, isoGridSquare.getX(), isoGridSquare.getY(), isoGridSquare.getZ(), false
                                );
                                if (isoGridSquare.getX() == this.x && isoGridSquare.getY() == this.y && isoGridSquare.getZ() == this.z
                                    || testResults != LosUtil.TestResults.Blocked) {
                                    float float0 = 0.0F;
                                    float float1;
                                    if (Math.abs(isoGridSquare.getZ() - this.z) <= 1) {
                                        float1 = IsoUtils.DistanceTo(
                                            (float)this.x, (float)this.y, 0.0F, (float)isoGridSquare.getX(), (float)isoGridSquare.getY(), 0.0F
                                        );
                                    } else {
                                        float1 = IsoUtils.DistanceTo(
                                            (float)this.x,
                                            (float)this.y,
                                            (float)this.z,
                                            (float)isoGridSquare.getX(),
                                            (float)isoGridSquare.getY(),
                                            (float)isoGridSquare.getZ()
                                        );
                                    }

                                    if (!(float1 > (float)this.radius)) {
                                        float0 = float1 / (float)this.radius;
                                        float0 = 1.0F - float0;
                                        float0 *= float0;
                                        if (this.life > -1) {
                                            float0 *= (float)this.life / (float)this.startlife;
                                        }

                                        float float2 = float0 * this.r * 2.0F;
                                        float float3 = float0 * this.g * 2.0F;
                                        float float4 = float0 * this.b * 2.0F;
                                        isoGridSquare.setLampostTotalR(isoGridSquare.getLampostTotalR() + float2);
                                        isoGridSquare.setLampostTotalG(isoGridSquare.getLampostTotalG() + float3);
                                        isoGridSquare.setLampostTotalB(isoGridSquare.getLampostTotalB() + float4);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        } else {
            this.bActive = false;
        }
    }

    public int getX() {
        return this.x;
    }

    public void setX(int _x) {
        this.x = _x;
    }

    public int getY() {
        return this.y;
    }

    public void setY(int _y) {
        this.y = _y;
    }

    public int getZ() {
        return this.z;
    }

    public void setZ(int _z) {
        this.z = _z;
    }

    public float getR() {
        return this.r;
    }

    public void setR(float _r) {
        this.r = _r;
    }

    public float getG() {
        return this.g;
    }

    public void setG(float _g) {
        this.g = _g;
    }

    public float getB() {
        return this.b;
    }

    public void setB(float _b) {
        this.b = _b;
    }

    public int getRadius() {
        return this.radius;
    }

    public void setRadius(int _radius) {
        this.radius = _radius;
    }

    public boolean isActive() {
        return this.bActive;
    }

    public void setActive(boolean _bActive) {
        this.bActive = _bActive;
    }

    public boolean wasActive() {
        return this.bWasActive;
    }

    public void setWasActive(boolean _bWasActive) {
        this.bWasActive = _bWasActive;
    }

    public ArrayList<IsoLightSwitch> getSwitches() {
        return this.switches;
    }

    public void setSwitches(ArrayList<IsoLightSwitch> _switches) {
        this.switches = _switches;
    }

    public void clearInfluence() {
        for (int int0 = this.x - this.radius; int0 < this.x + this.radius; int0++) {
            for (int int1 = this.y - this.radius; int1 < this.y + this.radius; int1++) {
                for (int int2 = 0; int2 < 8; int2++) {
                    IsoGridSquare isoGridSquare = IsoWorld.instance.CurrentCell.getGridSquare(int0, int1, int2);
                    if (isoGridSquare != null) {
                        isoGridSquare.setLampostTotalR(0.0F);
                        isoGridSquare.setLampostTotalG(0.0F);
                        isoGridSquare.setLampostTotalB(0.0F);
                    }
                }
            }
        }
    }

    public boolean isInBounds(int minX, int minY, int maxX, int maxY) {
        return this.x >= minX && this.x < maxX && this.y >= minY && this.y < maxY;
    }

    public boolean isInBounds() {
        IsoChunkMap[] isoChunkMap = IsoWorld.instance.CurrentCell.ChunkMap;

        for (int int0 = 0; int0 < IsoPlayer.numPlayers; int0++) {
            if (!isoChunkMap[int0].ignore) {
                int int1 = isoChunkMap[int0].getWorldXMinTiles();
                int int2 = isoChunkMap[int0].getWorldXMaxTiles();
                int int3 = isoChunkMap[int0].getWorldYMinTiles();
                int int4 = isoChunkMap[int0].getWorldYMaxTiles();
                if (this.isInBounds(int1, int3, int2, int4)) {
                    return true;
                }
            }
        }

        return false;
    }

    public boolean isHydroPowered() {
        return this.bHydroPowered;
    }

    public IsoBuilding getLocalToBuilding() {
        return this.localToBuilding;
    }
}
